package org.apache.struts2.plugin.json.writer;

import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import org.apache.struts2.plugin.json.Token;
import org.apache.struts2.plugin.json.model.JSONArray;
import org.apache.struts2.plugin.json.model.JSONValue;

/**
 * @author VERDOÏA Laurent <verdoialaurent@gmail.com>
 */
public class JSONArrayWriter extends AbstractJSONWriter<JSONArray> {

    public JSONArrayWriter(JSONArray content, OutputStream out) {
        super(content, out, Token.ARRAY_START, Token.ARRAY_END);
    }

    @Override
    public void writeContent() throws IOException {
        List<JSONValue> datas = this.content.getData();
        int max = datas.size() - 1;
        int i = 0;

        for (JSONValue value : datas) {
            AbstractJSONWriter ajw = JSONWriterFactory.create(value, out);
            ajw.write();
            if (i < max) {
                out.write(Token.COMMA.getBytes());
            }
            i++;
        }
    }
}
